GBML manual / Language

Objects and instances

An object is what you design; an instance is one of it that exists right now, with the variables the engine already gives it.

The difference

An object is what you build in the editor: a name, a set of events, a default sprite, how many of it may exist at once. It is a design, not a thing in the world. An instance is one concrete occurrence of an object, placed in a room or created by code, with its own position and its own copy of every instance variable. obj_enemy_walker is an object; the three walkers currently on screen are three instances of it, each with their own hp, x, y, and everything else - changing one does not touch the others.

var e = instance_create(x, y, obj_enemy_walker);
e.hp = 5;                       // only this new instance's hp changes

Every instance already has variables before your code runs

Before your own create event executes a single line, an instance already has the following, grouped by what they govern:

GroupVariables
Positionx, y, xprev, yprev, xstart, ystart
Motionhsp, vsp, speed, direction, gravity, gravity_dir, friction
Spritesprite, frame, frame_speed, frame_count (read-only), angle, xscale, yscale, flip_x, flip_y
Drawingvisible, depth
Collisionmask_sprite, solid, box_left, box_right, box_top, box_bottom
Lifetimepersistent, id, object
Timersalarm[0…11]
Pathspath, path_pos, path_speed, path_end_do

x/y is the instance's own position, counted from the room's top-left corner (see Where (0, 0) is) - the center of its collision mask, not a corner:

A sprite with its rectangular collision mask overlaidA sprite with its rectangular collision mask overlaid
A sprite with its rectangular collision mask overlaid

box_left/box_right/box_top/box_bottom are that mask's actual edges in world space, already offset by the current position - useful whenever you need to know exactly where an instance's edge is, rather than just where its center is. Where the machine shows through covers the mask itself: it is always a rectangle, whatever the sprite looks like.

self, other, and reaching another instance

Inside a plain event, self is the instance the event is running for - usually left implicit (hp -= 1; already means self.hp -= 1;). other only means something inside a with block or a Collision event:

References that know when their instance is gone

ref<obj_enemy_walker> is a typed reference: it either points at a living instance of that object, or it is nothing. Checking target != nothing is always safe and always accurate, even if the instance target used to point at was destroyed in the meantime - a stale reference reads back as nothing rather than pointing at whatever the engine later put in the same slot.

Inheritance

An object can have a parent (a field in the Code tab's own Object editor - see The Code tab). A child inherits the parent's instance variables, and a child object that does not write a given event inherits its parent's - one with no Step event of its own just runs its parent's. An event you DO write replaces the parent's for that child, even when yours is empty; to extend the parent's version instead, call event_call(parent) - Objects and parents walks through all of it. A parent's name also covers its children: with (parent_object) visits the parent's own instances and every child's, and so do instance_count, instance_nearest, instance_nth, instance_furthest and the overlap functions - "every kind of enemy" is just the shared parent's name. So does a Collision event: "Collision with obj_enemy" runs for every kind of enemy (Overlap and collision has the full list).

Creating and destroying

instance_create(x, y, obj) makes a new instance at a position and runs its Create event before the call returns, so code right after instance_create can already rely on the new instance being fully set up. instance_destroy() is not instant in the other direction: it only marks the instance. The instance finishes the current frame - the rest of its events still run, and other instances still find it - and at the end of the frame its Destroy event runs and the instance is removed. That is what makes it safe to call from inside a loop that is iterating over instances of that same object, without that loop skipping or double-visiting anything.

Choosing the object while the game runs

The object in instance_create, in with and in the other built-ins that ask for an object's name (instance_count, instance_nth, pos_free...) can also be a variable that holds an object, so a spawner can pick what to make, keep what it made and count them:

var kind = obj_zombie;
if (wave > 3) { kind = obj_brute; }

instance_create(x, y, kind);        // makes a zombie, or a brute once the wave is past 3
with (kind) { hp += 1; }            // every zombie (or every brute) gets one more hit point
var first_hp = kind.hp;             // the hit points of the first one alive, or 0 if there is none
var made = instance_create(x, y, kind);   // the answer can be kept: made.hp = 3; instance_destroy(made);
var alive = instance_count(kind);   // how many of that kind

A variable like this is a plain whole number inside the game (the place of the object in the list Gbabel makes of the objects your game keeps in variables), so it costs one number to store. See Keeping an object in a variable for what else it can do and what it cannot.

Instances are pooled, not unlimited

Every object declares the most instances of it that may exist at once, because the memory for all of them is reserved up front rather than allocated as you go. The editor turns what is actually placed into a per-room estimate against the hardware's real ceilings, but that estimate only ever knows about what is placed - creating an instance by code (instance_create) spends exactly the same real budget without the editor ever having counted it. See Budgets for what the editor's estimate can and cannot see, and how to check the true number instead.

An object only carries what it uses

An object's slots hold its own variables and the built-in ones its code can reach. The game is built knowing every object's code, so each object is given only the built-in parts it can ever use: the twelve alarm timers are cut down to the highest one the object (or anything that reaches it) sets, an object that nothing moves has no speed, direction, gravity or friction, an object that never follows a path or lights up a dark room has none of that, and the place it was created at and the place it was one step ago are kept only where something reads them. How fast its picture steps through its frames is kept only on an object whose code can set that speed, and the notes the engine keeps to draw an object's picture for it are kept for every slot only on an object that is drawn for you (an object with a Draw event of its own, or one that can never have a picture, keeps almost none). A wall, a tree or a picture on the wall is a few dozen bytes a slot; a player or an enemy that does all of these costs more. An object and its children share one choice, made for the whole family, because a child can be handled through its parent's name.

Nothing changes in how you write code. If you use a built-in on an object that did not have it (you start a path on an object that never had one), the game is built again with it, so it is always there when your code asks. The cost shows in the Budgets panel's memory figure, which counts each object by what it uses: adding a timer or a speed to an object raises the figure by what it costs.

See also: What GBML isEventsThe order of a frameStructs and constructorsRooms